package gui.nodes;

import gui.AbstractViewPane;
import gui.MainController;
import gui.UEUIConsulter;
import gui.AbstractViewPane.ViewMode;

import java.awt.event.*;
import javax.swing.*;

import utility.Record;
import business.SemestreBL;

public class UERoot extends AbstractNode {
	
	int semestreId;
	SemestreNode parentNode;
	
	public UERoot(SemestreNode parentNode, int semId){
		super();
		this.parentNode = parentNode;
		semestreId = semId;
	}

	public String getNodeName() {
		return "UE";
	}

	public void loadPrimaryViewPane() {
		//
	}
	
	public void loadAjouterViewPane() {
		AbstractViewPane newViewPane = new UEUIConsulter(parentNode,
AbstractViewPane.ViewMode.AJOUT, -1, semestreId);

		MainController.getInstance().setViewPane(newViewPane);
	}

	
	public JPopupMenu makePopup() {

		return new Popup();
	}
	
	private class Popup extends JPopupMenu implements ActionListener {
		JMenuItem ajouter = new JMenuItem("Ajouter une UE");
		
		public Popup() {
			ajouter.addActionListener(this);

			add(ajouter);
		}
		
		public void actionPerformed(ActionEvent e) {
			if (e.getSource() == ajouter){
				loadAjouterViewPane();
			}
			
		}
		
	}

	@Override
	public void expandHook() {
		this.removeAllChildren();
		
		SemestreBL semestreBL = new SemestreBL(semestreId);
		for (Record ueRec : semestreBL.getListeUE()) {
			UENode ueNode = new UENode(this, ueRec, semestreId);
			add(ueNode);
		}
		
	}

}
